package cn.gwm.flink.streaming.dwm.charge.util;

import cn.gwm.flink.streaming.constant.BaseFields;
import cn.gwm.flink.streaming.constant.BusinessUrl;
import cn.gwm.utils.ConfigLoader;
import cn.gwm.utils.GpsBean;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liangguang
 * @Date: 2022/8/10
 * @Description: 补充字段
 */
@Slf4j
@SuppressWarnings("all")
public class SupplyUtil {


    public static final String BUSINESS_STATION_URL = ConfigLoader.get("charging.station.type.url");
    public static final String CHARGING_ANXIETY_SOC_URL = ConfigLoader.get("charging.anxietySoc.url");
    public static final String CENTROID_CENTROID = ConfigLoader.get("gps.centroid.url");


    /**
     * 根据经纬度获取 充电站类型 和 城市完整编码
     *
     * @param latitude
     * @param longitude
     * @return
     */
    public static Map<String, String> supplyCode(BigDecimal latitude, BigDecimal longitude) {
        Map<String, String> result = new HashMap<>();
        if (latitude == null || BaseFields.defaultVal.equals(latitude.toString())) {
            return result;
        }
        String url = ConfigLoader.get("charging.station.type.url");
        Map<String, Object> param = new HashMap<>();
        param.put("latitude", latitude);
        param.put("longitude", longitude);

        try {
            String s = HttpUtil.get(BUSINESS_STATION_URL + BusinessUrl.STATION_URL, param);
            JSONObject parseObj = JSONUtil.parseObj(s);
            if (parseObj.getInt("code") == 200) {
                JSONObject resultData = parseObj.getJSONObject("data");
                if (resultData != null && resultData.size() > 0) {
                    result.put("adCode", resultData.getStr("adCode"));
                    // poiType  实际为高德的 aoiType
                    result.put("aoiType", resultData.getStr("poiType"));
                    result.put("aoiId", resultData.getStr("aoiId"));
                    result.put("poiType", resultData.getStr("poiTypeNameGroup"));
                    JSONArray pois = resultData.getJSONArray("pois");
                    if (pois != null && pois.size() > 0) {
                        JSONObject poiData = (JSONObject) pois.get(0);
                        result.put("poiId", poiData.getStr("id"));
                        if (StringUtils.isBlank(result.get("aoiType"))) {
                            result.put("aoiType", poiData.getStr("poiType"));
                        }
                        if (StringUtils.isBlank(result.get("poiType"))) {
                            result.put("poiType", poiData.getStr("poiTypeNameGroup"));
                        }
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" SupplyUtil  supplyCode happen error :" + e.getMessage() + "StackTrace={}", e.getStackTrace());
        }
        return result;
    }


    public static void main(String[] args) {
//        Map<String, BigDecimal> vinAndTid = getGpsByVinAndTid("dwd_location_v71phev", "LGW71PHEVRTK66666", 1668400000000L, 1668710000000L);
//        System.out.println(vinAndTid);
        // {aoiType=120302, adCode=340102, poiType=商务住宅|住宅区|住宅小区, aoiId=B022702XK8}
//        Map<String, String> stringMap = supplyCode(BigDecimal.valueOf(31.998385819912478), BigDecimal.valueOf(118.71613149074454));
//        System.out.println(stringMap);
//        System.out.println("--->>");
//        Map<String, String> stringMap2 = supplyPoi(BigDecimal.valueOf(32.88641717033258), BigDecimal.valueOf(120.32028954683128),
//                PoiTypeCodeEnum.CHARGING_STATION.getCode() + "|" + PoiTypeCodeEnum.RESIDENTIAL_DISTRICT.getCode());
//        System.out.println(stringMap2);

//        Map<String, Double> gps = getGpsByVinAndTid("dwd_location_v71phev", "LGWEFUA70MH000036", 1678430000000L, 1678430000000L);
//        System.out.println(gps);
//        Map<String, Double> gps = SupplyUtil.getGpsByVinAndTid(ConfigLoader.get("hbase.ods.table.name.gps.es13"), "test", System.currentTimeMillis() - 5000L, System.currentTimeMillis());
//        System.out.println(gps);

        String table = ConfigLoader.get("hbase.ods.table.name.gps.p03");
        String database = table.substring(0, table.indexOf(":"));
        String tableName = table.substring(table.indexOf(":") + 1, table.length());
        System.out.println(database);
        System.out.println(tableName);
//
        Map<String, Double> gps = SupplyUtil.getGpsByVinAndTid(database, tableName, "LGWFFUA60NM000008", 1682060000000L, 1682210000000L);
        System.out.println(gps);

    }

    /**
     * poi检索 ，poiTpey  类型 011100 充电站   120300 住宅小区
     *
     * @param latitude
     * @param longitude
     * @param poiType
     * @return
     */
    public static Map<String, String> supplyPoi(BigDecimal latitude, BigDecimal longitude, String poiTypes) {
        Map<String, String> result = new HashMap<>();
        if (latitude == null || BaseFields.defaultVal.equals(latitude.toString())) {
            return result;
        }
        String url = ConfigLoader.get("charging.station.type.url");
        Map<String, Object> param = new HashMap<>();
        param.put("latitude", latitude);
        param.put("longitude", longitude);
        param.put("poiTypes", poiTypes);
        try {
            String s = HttpUtil.get(BUSINESS_STATION_URL + BusinessUrl.POI_URL, param);
            JSONObject parseObj = JSONUtil.parseObj(s);
            if (parseObj.getInt("code") == 200) {
                JSONArray resultData = parseObj.getJSONArray("data");
                if (resultData.size() > 0) {
                    JSONObject poiData = (JSONObject) resultData.get(0);
                    result.put("poiId", poiData.getStr("aoiId"));
                    // 实际为 poiTypeCode = aoiType
                    result.put("aoiType", poiData.getStr("poiType"));
                    result.put("adCode", poiData.getStr("adCode"));
                    result.put("poiType", poiData.getStr("poiTypeNameGroup"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" SupplyUtil  supplyCode happen error :" + e.getMessage() + "StackTrace={}", e.getStackTrace());
        }
        return result;
    }


    /**
     * 获取业务端的电价信息列表
     *
     * @return
     */
    public static String getChargePriceInfoList() {
        String text = HttpUtil.get(String.format("%s%s", BUSINESS_STATION_URL, BusinessUrl.CHARGE_PRICE_URL));
        JSONObject parseObj = JSONUtil.parseObj(text);
        if (parseObj.getInt("code") == 200) {
            String resultData = parseObj.getStr("data");
            return resultData;
        }
        return null;
    }


    /**
     * 根据vin获取焦虑soc
     *
     * @return
     */
    public static Integer getAnxietySoc(String vin) {
        if (StringUtils.isBlank(vin)) {
            return null;
        }
        String text = HttpUtil.get(String.format("%s%s?deviceid=%s", CHARGING_ANXIETY_SOC_URL, BusinessUrl.ANXIETY_SOC_UUL, vin));
        JSONObject parseObj = JSONUtil.parseObj(text);
        if (parseObj.getInt("err_no") == 0) {
            JSONObject data = (JSONObject) parseObj.getObj("data");
            if (data.getInt("jiaolv_soc") != null) {
                return data.getInt("jiaolv_soc");
            }
        }
        return null;

    }

    /**
     * 根据vin 获取经纬度信息
     *
     * @param phoenixTable 经纬度 视图名称
     * @return
     */
    public static Map<String, Double> getGpsByVinAndTid(String phoenixTable, String vin, Long startTime, Long endTime) {
        Map<String, Object> param = new HashMap<>();
        param.put("vin", vin);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("hbaseTableName", phoenixTable);
        Map<String, Double> result = new HashMap<>();
        try {
            String s = HttpUtil.get(BUSINESS_STATION_URL + BusinessUrl.QUERY_GPS_BY_VIN, param);
            JSONObject parseObj = JSONUtil.parseObj(s);
            if (parseObj.getInt("code") == 200) {
                JSONArray resultList = parseObj.getJSONArray("data");
                if (resultList.size() > 0) {
                    JSONObject obj = (JSONObject) resultList.get(0);
                    result.put("latitude", obj.getDouble("latitude"));
                    result.put("longitude", obj.getDouble("longitude"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }


    /**
     * 根据vin 获取经纬度信息
     *
     * @param phoenixTable 经纬度 视图名称
     * @return
     */
    public static Map<String, Double> getGpsByVinAndTid(String dataBase, String phoenixTable, String vin, Long startTime, Long endTime) {
        Map<String, Object> param = new HashMap<>();
        param.put("vin", vin);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        param.put("hbaseTableName", phoenixTable);
        param.put("dataBase", dataBase);

        Map<String, Double> result = new HashMap<>();
        try {
            String s = HttpUtil.get(BUSINESS_STATION_URL + BusinessUrl.QUERY_GPS_BY_VIN, param);
            JSONObject parseObj = JSONUtil.parseObj(s);
            if (parseObj.getInt("code") == 200) {
                JSONArray resultList = parseObj.getJSONArray("data");
                if (resultList.size() > 0) {
                    JSONObject obj = (JSONObject) resultList.get(0);
                    result.put("latitude", obj.getDouble("latitude"));
                    result.put("longitude", obj.getDouble("longitude"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
        return result;
    }


    /**
     * 去除异常点 求质心
     *
     * @param gpsList
     * @return
     */
    public static GpsBean getCentroid(List<List<String>> gpsList) {
        String url = CENTROID_CENTROID + BusinessUrl.QUERY_CORE;
        Map<String, Object> map = new HashMap<>();
        map.put("gps_list", gpsList);
        try {
            String postResult = HttpUtil.post(url, JSONUtil.toJsonStr(map));
            JSONObject result = JSONUtil.parseObj(postResult);
            if (0 == result.getInt("err_no")) {
                JSONObject data = result.getJSONObject("data");
                JSONArray zhongxin = data.getJSONArray("zhongxin");
                if (zhongxin.size() > 0) {
                    GpsBean bean = new GpsBean(Double.valueOf(zhongxin.get(0) + ""), Double.valueOf(zhongxin.get(1) + ""));
                    return bean;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" SupplyUtil  getCentroid happen error :" + e.getMessage() + "StackTrace={}", e.getStackTrace());
        }
        return null;
    }


    /**
     * 根据vin 获取经纬度信息
     *
     * @return
     */
    public static Map<String, BigDecimal> getGpsByVinAndTid(String vin, Long startTime, Long endTime) {
        Map<String, Object> param = new HashMap<>();
        param.put("vin", vin);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        Map<String, BigDecimal> result = new HashMap<>();
        try {
            String s = HttpUtil.get(BUSINESS_STATION_URL + BusinessUrl.QUERY_GPS_BY_VIN, param);
            JSONObject parseObj = JSONUtil.parseObj(s);
            if (parseObj.getInt("code") == 200) {
                JSONArray resultList = parseObj.getJSONArray("data");
                if (resultList.size() > 0) {
                    JSONObject obj = (JSONObject) resultList.get(0);
                    result.put("latitude", obj.getBigDecimal("latitude"));
                    result.put("longitude", obj.getBigDecimal("longitude"));
                }
            }
        } catch (Exception e) {
            return result;
        }
        return result;
    }

}
